package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.Servlet;
import javax.servlet.http.HttpSession;
import java.security.Provider;
import java.util.List;

public class UserContext {
    //把USER_IN_SESSION和EXPRESSION_IN_SESSION抽取出来
    public static final String USER_IN_SESSION = "USER_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";

    /**
     *
     //springmvc提供了工具类,让我们在任意地方直接就可以获取到session,request,response
     //springmvc获取session工具类可以通过百度搜索
     * @return
     */
    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();//(ServletRequestAttributes)目的转换成实现类
        return requestAttributes.getRequest().getSession();
    }
    /**
     * 把员工存放到session中
     */
    public static void setCurrentUser(Employee employee){

        getSession().setAttribute(USER_IN_SESSION,employee);
    }

    /**
     * 从session中获取当前登录用户
     * @return
     */
    public static Employee getCurrentUser(){
        return (Employee) getSession().getAttribute(USER_IN_SESSION);
    }


    /**
     * 把权限表达式存放到session中
     */
    public static void setExpressions(List<String> expressions){
        getSession().setAttribute(EXPRESSION_IN_SESSION,expressions);
    }

    /**
     * 从session中获取权限表达式集合
     * @return
     */
    public static List<String> getExpressions(){
        return (List<String>) getSession().getAttribute(EXPRESSION_IN_SESSION);
    }
}
